{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Gradient Boosting Classification\n",
    "\n",
    "The Gradient Boosting (GB) algorithm trains a series of weak learners and each focuses on the errors the previous learners have made and tries to improve it. Together, they make a better prediction.\n",
    "\n",
    "According to Wikipedia, Gradient boosting is a machine learning technique for regression and classification problems, which produces a prediction model in the form of an ensemble of weak prediction models, typically decision trees. It builds the model in a stage-wise fashion as other boosting methods do, and it generalizes them by allowing optimization of an arbitrary differentiable loss function. \n",
    "\n",
    "Prerequisite\n",
    "\n",
    "    1. Linear regression and gradient descent\n",
    "    2. Decision Tree\n",
    "    3. Gradient Boosting Regression\n",
    "\n",
    "After studying this post, you will be able to:\n",
    "\n",
    "    1. Explain gradient boosting algorithm.\n",
    "    2. Explain gradient boosting classification algorithm.\n",
    "    3. Write a gradient boosting classification from scratch\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "# The algorithm\n",
    "\n",
    "The following plot illustrates the algorithm.\n",
    "\n",
    "![Gradient Boosting Regression](gradient_boosting/gradient_boosting_classification.png)\n",
    "\n",
    "(Picture taken from Youtube channel StatQuest)\n",
    "\n",
    "From the plot above, the first part is a stump, which is the log of odds of **y**. We then add several trees to it. In the following trees, the target is not y. Instead, the target is the residual or the true value subtracts the previous prediction.\n",
    "\n",
    "$$residual=true\\_value - previous\\_prediction$$\n",
    "\n",
    "That is why we say in Gradient Boosting trains a series of weak learners, each focuses on the errors of the previous one. The residual predictions are multiplied by the learning rate (0.1 here) before added to the average.\n",
    "\n",
    "Here the picture looks more complicated than the one on regression. The purple ones are log of odds (**l**). The green ones are probabilities. We firstly calculate log of odds of **y**, instead of average. We then calculate probabilities using log of odds. We build a regression tree. The leaves are colored green. The leaves have residuals. We use the probability residuals to produce log-of-odds residuals or $\\gamma$. $\\gamma$ is then used to update **l**. This continues until we are satisfied with the results or we are running out of iterations.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "**The Steps**\n",
    "\n",
    "Step 1: Calculate the log of odds of y. This is also the first estimation of y. Here $n_1$ is the number of true values and $n_0$ of false values.\n",
    "$$l_0(x)=\\log \\frac{n_1}{n_0}$$\n",
    "For each $x_i$, the probability is:\n",
    "$$p_{0i}=\\frac{e^{l_{0i}}}{1+e^{l_{0i}}} $$\n",
    "The prediction is:\n",
    "$$\n",
    "f_{0i}=\\begin{cases}\n",
    "0 & p_{0i}<0.5 \\\\\n",
    "1 & p_{0i}>=0.5\n",
    "\\end{cases}\n",
    "$$\n",
    "\n",
    "Step 2 for m in 1 to M: <br />\n",
    "  * Step 2.1: Compute so-call pseudo-residuals:\n",
    "    $$r_{im}=f_i-p_i$$\n",
    "  * Step 2.2: Fit a regression tree $t_m(x)$ to pseudo-residuals and create terminal regions (leaves) $R_{jm}$ for $j=1...Jm$ <br />\n",
    "\n",
    "  * Step 2.3: For each leaf of the tree, there are $p_j$ elements, compute $\\gamma$ as following equation. <br />\n",
    "\n",
    "$$\\gamma_{im}=\\frac{\\sum r_{im}}{\\sum (1-r_{im-1})(r_{im-1})} $$\n",
    "\n",
    "  * (In practise, the regression tree will do this for us.)\n",
    "\n",
    "  * Step 2.4: Update the log of odds with learning rate $\\alpha$:\n",
    "$$l_m(x)=l_{m-1}+\\alpha \\gamma_m$$\n",
    "\n",
    "For each $x_i$, the probability is:\n",
    "$$p_{mi}=\\frac{e^{l_{mi}}}{1+e^{l_{mi}}} $$\n",
    "The prediction is:\n",
    "$$\n",
    "f_{mi}=\\begin{cases}\n",
    "0 & p_{mi}<0.5 \\\\\n",
    "1 & p_{mi}>=0.5\n",
    "\\end{cases}\n",
    "$$\n",
    "\n",
    "\n",
    "Step 3. Output $$f_M(x)$$\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# (Optional) From Gradient Boosting to Gradient Boosting Regression\n",
    "\n",
    "The above knowledge is enough for writing BGR code from scratch. But I want to explain more about gradient boosting. GB is a meta-algorithm that can be applied to both regression and classification. The above one is only a specific form for regression. In the following, I will introduce the general gradient boosting algorithm and deduce GBR from GB.\n",
    "\n",
    "Let's first look at the GB steps\n",
    "\n",
    "---\n",
    "\n",
    "**The Steps**\n",
    "\n",
    "Input: training set $\\{(x_i, y_i)\\}_{i=1}^{n}$, a differentiable loss function $L(y, F(x))$, number of iterations M\n",
    "\n",
    "Algorithm:\n",
    "\n",
    "Step 1: Initialize model with a constant value:\n",
    "\n",
    "$$F_0(x)=\\underset{\\gamma}{\\operatorname{argmin}}\\sum_{i=1}^{n}L(y_i, \\gamma)$$\n",
    "\n",
    "Step 2 for m in 1 to M: <br />\n",
    "  * Step 2.1: Compute so-call pseudo-residuals:\n",
    "    $$r_{im}=-[\\frac{\\partial L(y_i, F(x_i))}{\\partial F(x_i)}], {F(x)=F_{m-1}(x)}$$\n",
    "  * Step 2.2: Fit a weak learner $h_m(x)$ to pseudo-residuals. and create terminal regions $R_{jm}$, for $j=1...J_m$<br />\n",
    "\n",
    "  * Step 2.3: For each leaf of the tree, compute $\\gamma$ as the following equation. Here $\\hat{r}$ is the predicted residual produced by $h_m(x)$.<br />\n",
    "$$\\gamma_{jm}=\\underset{\\gamma}{\\operatorname{argmin}}\\sum_{x_i \\in R_{jm}}^{n}L(y_i, F_{m-1}(x_i)+\\gamma)$$\n",
    "\n",
    "  * Step 2.4: Update the model with learning rate $\\alpha$:\n",
    "$$F_m(x)=F_{m-1}+\\alpha\\gamma_m$$\n",
    "\n",
    "\n",
    "Step 3. Output $$F_M(x)$$\n",
    "\n",
    "Lost Function:\n",
    "\n",
    "To deduce the GB to GBC, I simply define a loss function and solve the loss function in step 1, 2.1 and 2.3. We use Log of Likelihood as the loss function:\n",
    "\n",
    "$$L(y, F(x))=-\\sum_{i=1}^{N}(y_i* log(p) + (1-y_i)*log(1-p))$$\n",
    "\n",
    "Since this is a function of probability and we need a function of log of odds($l$), let's focus on the middle part and transform it into a function of $l$.\n",
    "\n",
    "The middle part is:\n",
    "\n",
    "$$\n",
    "-(y*\\log(p)+(1-y)*\\log(1-p)) \\\\\n",
    "=-y * \\log(p) - (1-y) * \\log(1-p) \\\\\n",
    "=-y\\log(p)-\\log(1-p)+y\\log(1-p) \\\\\n",
    "=-y(\\log(p)-\\log(1-p))-\\log(1-p) \\\\\n",
    "=-y(\\log(\\frac{p}{1-p}))-\\log(1-p) \\\\\n",
    "=-y \\log(odds)-\\log(1-p)\n",
    "$$\n",
    "\n",
    "Since\n",
    "\n",
    "$$\n",
    "\\log(1-p)=log(1-\\frac{e^{log(odds)}}{1+e^{log(odds)}}) \\\\\n",
    "=\\log(\\frac{1+e^l}{1+e^l}-\\frac{e^l}{1+e^l})\\\\\n",
    "=\\log(\\frac{1}{1+e^l}) \\\\\n",
    "=\\log(1)+\\log(1+e^l) \\\\\n",
    "=-log(1+e^{\\log(odds)})\n",
    "$$\n",
    "\n",
    "We put this to the previous equation:\n",
    "$$\n",
    "-(y*\\log(p)+(1-y)*\\log(1-p)) \\\\\n",
    "=-y\\log(odds)+\\log(1+e^{\\log(odds)}) \\\\\n",
    "$$\n",
    "Thus, we will have the loss function over log of odds:\n",
    "\n",
    "$$L=-\\sum_{i=1}^{N}(y\\log(odds)-\\log(1+e^{\\log(odds)}))$$\n",
    "\n",
    "\n",
    "\n",
    "For Step 1:\n",
    "\n",
    "Because The lost function is convex and at the lowest point where the derivative is zero, we have the following:\n",
    "\n",
    "$$\n",
    "\\frac{\\partial L(y, F_0)}{\\partial F_0}  \\\\\n",
    "=-\\frac{\\partial \\sum_{i=1}^{N}(y\\log(odds)-\\log(1+e^{\\log(odds)}))}{\\partial log(odds)} \\\\\n",
    "=-\\sum_{i=1}^{n} y_i+\\sum_{i=1}^{N} \\frac{\\partial log(1+e^{log(odds)})}{\\partial log(odds)} \\\\\n",
    "=-\\sum_{i=1}^{n} y_i+\\sum_{i=1}^{N} \\frac{1}{1+e^{\\log(odds)}} \\frac{\\partial (1+e^l)}{\\partial l} \\\\\n",
    "=-\\sum_{i=1}^{n} y_i+\\sum_{i=1}^{N} \\frac{1}{1+e^{\\log(odds)}} \\frac{\\partial (e^l)}{\\partial l} \\\\\n",
    "=-\\sum_{i=1}^{n} y_i+\\sum_{i=1}^{N} \\frac{e^l}{1+e^l} \\\\\n",
    "=-\\sum_{i=1}^{n} y_i+N\\frac{e^l}{1+e^l}=0\n",
    "$$\n",
    "\n",
    "And We have: (Here p is the real probability)\n",
    "\n",
    "\n",
    "$$\n",
    "\\frac{e^l}{1+e^l}=\\frac{\\sum_{i=1}^{N}y_i}{N}=p \\\\\n",
    "e^l=p+p*e^l \\\\\n",
    "(1-p)e^l=p \\\\\n",
    "e^l=\\frac{p}{1-p} \\\\\n",
    "\\log(odds)=log(\\frac{p}{1-p})\n",
    "$$\n",
    "\n",
    "Such that, when log(odds)=log(p/(1-p)) or the probability is the real probability, the lost function is minimized. \n",
    "\n",
    "For Step 2.1\n",
    "\n",
    "$$r_{im}=-[\\frac{\\partial L(y_i, F(x_i))}{\\partial F(x_i)}]_{F(x)=F_{m-1}(x)}$$\n",
    "\n",
    "$$=-[\\frac{\\partial (-(y_i* log(p)+(1-y_i)*log(1-p)))}{\\partial F_{m-1}(x_i)}]_{F(x)=F_{m-1}(x)}$$\n",
    "\n",
    "We have already taken the derivative.\n",
    "\n",
    "$$=y_i-F_{m-1}(x_i)$$\n",
    "\n",
    "For step 2.3:\n",
    "\n",
    "$$\\gamma_{jm}=\\underset{\\gamma}{\\operatorname{argmin}}\\sum_{x_i \\in R_{jm}}^{n}L(y_i, F_{m-1}(x_i)+\\gamma)$$\n",
    "\n",
    "I apply the lost function:\n",
    "\n",
    "$$\n",
    "\\gamma_{jm} \\\\\n",
    "=\\underset{\\gamma}{\\operatorname{argmin}}\\sum_{x_i \\in R_{jm}}^{n}L(y_i, F_{m-1}(x_i)+\\gamma) \\\\\n",
    "=\\underset{\\gamma}{\\operatorname{argmin}}\\sum_{x_i \\in R_{jm}}^{n} (-y_i * (F_{m-1}+\\gamma)+\\log(1+e^{F_{m-1}+\\gamma})) \\\\\n",
    "$$\n",
    "Let's focus on the middle part\n",
    "\n",
    "$$-y_i * (F_{m-1}+\\gamma)+\\log(1+e^{F_{m-1}+\\gamma})$$\n",
    "\n",
    "Let's use Second Order Taylor Polynomial:\n",
    "\n",
    "$$L(y,F+\\gamma) \\approx L(y, F)+ \\frac{d L(y, F+\\gamma)\\gamma}{d F}+\\frac{1}{2} \\frac{d^2 L(y, F+\\gamma)\\gamma^2}{d^2 F}$$\n",
    "\n",
    "Let's take the derivate:\n",
    "\n",
    "$$\n",
    "\\because \\frac{d L(y, F+\\gamma)}{d\\gamma} \\approx \\frac{d L(y, F)}{d F}+\\frac{d^2 L(y, F)\\gamma}{d^2 F}=0 \\\\\n",
    "\\therefore \\frac{d L(y, F)}{d F}+\\frac{d^2 L(y, F)\\gamma}{d^2 F}=0 \\\\\n",
    "\\therefore \\gamma=-\\frac{\\frac{d L(y, F)}{d F}}{\\frac{d^2 L(y, F)}{d^2 F}} \\\\\n",
    "\\therefore \\gamma = \\frac{y-p}{\\frac{d^2 (-y * l + \\log(1+e^l))}{d^2 l}} \\\\\n",
    "\\therefore \\gamma = \\frac{y-p}{\\frac{d (-y + \\frac{e^l}{1+e^l})}{d l}} \\\\\n",
    "\\therefore \\gamma = \\frac{y-p}{\\frac{d \\frac{e^l}{1+e^l}}{d l}} \\\\\n",
    "$$\n",
    "(The product rule (ab)'=a' b+a b'​)\n",
    "\n",
    "$$\n",
    "\\therefore \\gamma=\\frac{y-p}{\\frac{d e^l}{dl} * \\frac{1}{1+e^l} - e^l * \\frac{d }{d l} \\frac{1}{1+e^l}} \\\\\n",
    "=\\frac{y-p}{\\frac{e^l}{1+e^l}-e^l * \\frac{1}{(1+e^l)^2} \\frac{d}{dl} (1+e^l)} \\\\\n",
    "=\\frac{y-p}{\\frac{e^l}{1+e^l}- \\frac{(e^l)^2}{(1+e^l)^2}} \\\\\n",
    "=\\frac{y-p}{e^l+(e^l)^2-+(e^l)^2} \\\\\n",
    "=\\frac{y-p}{\\frac{e^l}{(1+e^l)^2}} \\\\\n",
    "=\\frac{y-p}{p(1-p)}\n",
    "$$\n",
    "\n",
    "\n",
    "Now we have\n",
    "\n",
    "$$\\gamma = \\frac{\\sum (y-p)}{\\sum p(1-p)}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeRegressor\n",
    "from sklearn.datasets import load_breast_cancer\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "import graphviz \n",
    "from sklearn import tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>likes_popcorn</th>\n",
       "      <th>age</th>\n",
       "      <th>favorite_color</th>\n",
       "      <th>loves_troll2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alex</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Brunei</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Candy</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Blue</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>David</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>Red</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eric</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felicity</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name  likes_popcorn  age favorite_color  loves_troll2\n",
       "0      Alex              1   10           Blue             1\n",
       "1    Brunei              1   90          Green             1\n",
       "2     Candy              0   30           Blue             0\n",
       "3     David              1   30            Red             0\n",
       "4      Eric              0   30          Green             1\n",
       "5  Felicity              0   10           Blue             1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>likes_popcorn</th>\n",
       "      <th>age</th>\n",
       "      <th>blue</th>\n",
       "      <th>green</th>\n",
       "      <th>red</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   likes_popcorn  age  blue  green  red\n",
       "0              1   10     1      0    0\n",
       "1              1   90     1      0    0\n",
       "2              0   30     1      0    0\n",
       "3              1   30     1      0    0\n",
       "4              0   30     1      0    0\n",
       "5              0   10     1      0    0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df=pd.DataFrame()\n",
    "df['name']=['Alex','Brunei','Candy','David','Eric','Felicity']\n",
    "df['likes_popcorn']=[1,1,0,1,0,0]\n",
    "df['age']=[10,90,30,30,30,10]\n",
    "df['favorite_color']=['Blue','Green','Blue','Red','Green','Blue']\n",
    "df['loves_troll2']=[1,1,0,0,1,1]\n",
    "print('Data:')\n",
    "display(df)\n",
    "\n",
    "X=df[['likes_popcorn','age']].copy()\n",
    "X['blue']=0\n",
    "X.loc[df['favorite_color']=='Blue','blue']=1\n",
    "X['green']=0\n",
    "X.loc[df['favorite_color']=='Green','blue']=1\n",
    "X['red']=0\n",
    "X.loc[df['favorite_color']=='Red','blue']=1\n",
    "\n",
    "y=df['loves_troll2']\n",
    "print('X:')\n",
    "display(X)\n",
    "\n",
    "n=df.shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1 Log of Odds, Probabilities and Predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 292,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the log_of_odds is : 0.6931471805599453\n",
      "the probability is : 0.6666666666666666\n",
      "the prediction is : 1\n"
     ]
    }
   ],
   "source": [
    "log_of_odds0=np.log(4 / 2)\n",
    "probability0=np.exp(log_of_odds0)/(np.exp(log_of_odds0)+1)\n",
    "print(f'the log_of_odds is : {log_of_odds0}')\n",
    "print(f'the probability is : {probability0}')\n",
    "predict0=1\n",
    "print(f'the prediction is : 1')\n",
    "n_samples=6\n",
    "\n",
    "loss0=-(y*np.log(probability0)+(1-y)*np.log(1-probability0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 293,
   "metadata": {},
   "outputs": [],
   "source": [
    "#now let's get started\n",
    "n_times = 5\n",
    "learning_rate=0.8\n",
    "log_of_odds=np.zeros([n_times+1,n_samples])\n",
    "\n",
    "residuals = np.zeros([n_times+1,n_samples])\n",
    "loss = [0] * (n_times+1)\n",
    "probabilities = np.zeros([n_times+1,n_samples])\n",
    "predictions = np.zeros([n_times+1,n_samples])\n",
    "score = np.zeros([n_times+1])\n",
    "#calculation\n",
    "log_of_odds[0] = [log_of_odds0] * n_samples\n",
    "probabilities[0] = [probability0] * n_samples\n",
    "predictions[0] = (probabilities[0]>0.5)*1.0\n",
    "score[0]=np.sum(predictions[0]==y) / n_samples\n",
    "residuals[0] = y - probabilities[0]\n",
    "loss[0] = loss0.sum()\n",
    "trees = []\n",
    "gamma_value = np.zeros([n_times+1,8])\n",
    "gamma = np.zeros([n_times+1,n_samples])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 6)"
      ]
     },
     "execution_count": 294,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "residuals.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 6)"
      ]
     },
     "execution_count": 295,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "probabilities.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {},
   "outputs": [],
   "source": [
    "def iteration(i):\n",
    "    #step 2.1 calculate the residuals\n",
    "    residuals[i] = y - probabilities[i]\n",
    "    #step 2.2 Fit a regression tree\n",
    "    dt = DecisionTreeRegressor(max_depth=1, max_leaf_nodes=3)\n",
    "    dt=dt.fit(X, residuals[i])\n",
    "    \n",
    "    trees.append(dt.tree_)\n",
    "    \n",
    "    #Step 2.3 Calculate gamma\n",
    "    leaf_indeces=dt.apply(X)\n",
    "    print(leaf_indeces)\n",
    "    unique_leaves=np.unique(leaf_indeces)\n",
    "    n_leaf=len(unique_leaves)\n",
    "    #for leaf 1\n",
    "    for ileaf in range(n_leaf):\n",
    "        \n",
    "        leaf_index=unique_leaves[ileaf]\n",
    "        n_leaf=len(leaf_indeces[leaf_indeces==leaf_index])\n",
    "        previous_probability = probabilities[i][leaf_indeces==leaf_index]\n",
    "        denominator = np.sum(previous_probability * (1-previous_probability))\n",
    "        igamma = dt.tree_.value[ileaf+1][0][0] * n_leaf / denominator\n",
    "        gamma_value[i][ileaf]=igamma\n",
    "        print(f'for leaf {leaf_index}, we have {n_leaf} related samples. and gamma is {igamma}')\n",
    "\n",
    "    gamma[i] = [gamma_value[i][np.where(unique_leaves==index)] for index in leaf_indeces]\n",
    "    #Step 2.4 Update F(x) \n",
    "    log_of_odds[i+1] = log_of_odds[i] + learning_rate * gamma[i]\n",
    "\n",
    "    probabilities[i+1] = np.array([np.exp(odds)/(np.exp(odds)+1) for odds in log_of_odds[i+1]])\n",
    "    predictions[i+1] = (probabilities[i+1]>0.5)*1.0\n",
    "    score[i+1]=np.sum(predictions[i+1]==y) / n_samples\n",
    "    #residuals[i+1] = y - probabilities[i+1]\n",
    "    loss[i+1]=np.sum(-y * log_of_odds[i+1] + np.log(1+np.exp(log_of_odds[i+1])))\n",
    "    \n",
    "    new_df=df.copy()\n",
    "    new_df.columns=['name', 'popcorn','age','color','y']\n",
    "    new_df[f'$p_{i}$']=probabilities[i]\n",
    "    new_df[f'$l_{i}$']=log_of_odds[i]\n",
    "    new_df[f'$r_{i}$']=residuals[i]\n",
    "    new_df[f'$\\gamma_{i}$']=gamma[i]\n",
    "    new_df[f'$l_{i+1}$']=log_of_odds[i+1]\n",
    "    new_df[f'$p_{i+1}$']=probabilities[i+1]\n",
    "    display(new_df)\n",
    "    \n",
    "    dot_data = tree.export_graphviz(dt, out_file=None, filled=True, rounded=True,feature_names=X.columns) \n",
    "    graph = graphviz.Source(dot_data) \n",
    "    display(graph)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 2 2 2 1]\n",
      "for leaf 1, we have 2 related samples. and gamma is 1.5\n",
      "for leaf 2, we have 4 related samples. and gamma is -0.7499999999999998\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>popcorn</th>\n",
       "      <th>age</th>\n",
       "      <th>color</th>\n",
       "      <th>y</th>\n",
       "      <th>$p_0$</th>\n",
       "      <th>$l_0$</th>\n",
       "      <th>$r_0$</th>\n",
       "      <th>$\\gamma_0$</th>\n",
       "      <th>$l_1$</th>\n",
       "      <th>$p_1$</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alex</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>1.50</td>\n",
       "      <td>1.893147</td>\n",
       "      <td>0.869114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Brunei</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>-0.75</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>0.523270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Candy</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Blue</td>\n",
       "      <td>0</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>-0.666667</td>\n",
       "      <td>-0.75</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>0.523270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>David</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>Red</td>\n",
       "      <td>0</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>-0.666667</td>\n",
       "      <td>-0.75</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>0.523270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eric</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>-0.75</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>0.523270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felicity</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.693147</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>1.50</td>\n",
       "      <td>1.893147</td>\n",
       "      <td>0.869114</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name  popcorn  age  color  y     $p_0$     $l_0$     $r_0$  $\\gamma_0$  \\\n",
       "0      Alex        1   10   Blue  1  0.666667  0.693147  0.333333        1.50   \n",
       "1    Brunei        1   90  Green  1  0.666667  0.693147  0.333333       -0.75   \n",
       "2     Candy        0   30   Blue  0  0.666667  0.693147 -0.666667       -0.75   \n",
       "3     David        1   30    Red  0  0.666667  0.693147 -0.666667       -0.75   \n",
       "4      Eric        0   30  Green  1  0.666667  0.693147  0.333333       -0.75   \n",
       "5  Felicity        0   10   Blue  1  0.666667  0.693147  0.333333        1.50   \n",
       "\n",
       "      $l_1$     $p_1$  \n",
       "0  1.893147  0.869114  \n",
       "1  0.093147  0.523270  \n",
       "2  0.093147  0.523270  \n",
       "3  0.093147  0.523270  \n",
       "4  0.093147  0.523270  \n",
       "5  1.893147  0.869114  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"236pt\" height=\"165pt\"\r\n",
       " viewBox=\"0.00 0.00 235.50 165.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 161)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-161 231.5,-161 231.5,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#f6d5bd\" stroke=\"black\" d=\"M148.5,-157C148.5,-157 75.5,-157 75.5,-157 69.5,-157 63.5,-151 63.5,-145 63.5,-145 63.5,-101 63.5,-101 63.5,-95 69.5,-89 75.5,-89 75.5,-89 148.5,-89 148.5,-89 154.5,-89 160.5,-95 160.5,-101 160.5,-101 160.5,-145 160.5,-145 160.5,-151 154.5,-157 148.5,-157\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">age &lt;= 20.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.222</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 6</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.0</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M90,-53C90,-53 12,-53 12,-53 6,-53 0,-47 0,-41 0,-41 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 90,-0 90,-0 96,-0 102,-6 102,-12 102,-12 102,-41 102,-41 102,-47 96,-53 90,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.333</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M90.6652,-88.9485C84.9963,-80.1664 78.8618,-70.6629 73.1504,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"75.977,-59.7402 67.6131,-53.2367 70.0959,-63.5365 75.977,-59.7402\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"62.3481\" y=\"-73.976\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M215.5,-53C215.5,-53 132.5,-53 132.5,-53 126.5,-53 120.5,-47 120.5,-41 120.5,-41 120.5,-12 120.5,-12 120.5,-6 126.5,-0 132.5,-0 132.5,-0 215.5,-0 215.5,-0 221.5,-0 227.5,-6 227.5,-12 227.5,-12 227.5,-41 227.5,-41 227.5,-47 221.5,-53 215.5,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.25</text>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = &#45;0.167</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>0&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M133.685,-88.9485C139.446,-80.1664 145.681,-70.6629 151.486,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"154.555,-63.5178 157.115,-53.2367 148.703,-59.6779 154.555,-63.5178\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"162.198\" y=\"-74.0145\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x1f165fa0688>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iteration(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Iteration 0, Let's look at each step.\n",
    "\n",
    "In Step 2.1, We calculate residuals, that is $y-p_0$. \n",
    "\n",
    "In step 2.2, we fit a regression tree as above.\n",
    "\n",
    "In step 2.3, we calculate $\\gamma$. \n",
    "\n",
    "  * For the first leaf, we have two samples (Alex and Felicity). $\\gamma$ is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4999999999999998"
      ]
     },
     "execution_count": 300,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1/3+1/3)/((1-2/3)*2/3+(1-2/3)*2/3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "  * For the second leaf, we have four samples. $\\gamma$ is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.75"
      ]
     },
     "execution_count": 301,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1/3-2/3-2/3+1/3)/(4*(1-2/3)*2/3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Step 2.4, F(x) is updated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 1 1 1 1]\n",
      "for leaf 1, we have 5 related samples. and gamma is -0.31564962030401844\n",
      "for leaf 2, we have 1 related samples. and gamma is 1.9110594001952543\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>popcorn</th>\n",
       "      <th>age</th>\n",
       "      <th>color</th>\n",
       "      <th>y</th>\n",
       "      <th>$p_1$</th>\n",
       "      <th>$l_1$</th>\n",
       "      <th>$r_1$</th>\n",
       "      <th>$\\gamma_1$</th>\n",
       "      <th>$l_2$</th>\n",
       "      <th>$p_2$</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alex</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.869114</td>\n",
       "      <td>1.893147</td>\n",
       "      <td>0.130886</td>\n",
       "      <td>-0.315650</td>\n",
       "      <td>1.640627</td>\n",
       "      <td>0.837620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Brunei</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.523270</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>0.476730</td>\n",
       "      <td>1.911059</td>\n",
       "      <td>1.621995</td>\n",
       "      <td>0.835070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Candy</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Blue</td>\n",
       "      <td>0</td>\n",
       "      <td>0.523270</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>-0.523270</td>\n",
       "      <td>-0.315650</td>\n",
       "      <td>-0.159373</td>\n",
       "      <td>0.460241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>David</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>Red</td>\n",
       "      <td>0</td>\n",
       "      <td>0.523270</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>-0.523270</td>\n",
       "      <td>-0.315650</td>\n",
       "      <td>-0.159373</td>\n",
       "      <td>0.460241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eric</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.523270</td>\n",
       "      <td>0.093147</td>\n",
       "      <td>0.476730</td>\n",
       "      <td>-0.315650</td>\n",
       "      <td>-0.159373</td>\n",
       "      <td>0.460241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felicity</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.869114</td>\n",
       "      <td>1.893147</td>\n",
       "      <td>0.130886</td>\n",
       "      <td>-0.315650</td>\n",
       "      <td>1.640627</td>\n",
       "      <td>0.837620</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name  popcorn  age  color  y     $p_1$     $l_1$     $r_1$  $\\gamma_1$  \\\n",
       "0      Alex        1   10   Blue  1  0.869114  1.893147  0.130886   -0.315650   \n",
       "1    Brunei        1   90  Green  1  0.523270  0.093147  0.476730    1.911059   \n",
       "2     Candy        0   30   Blue  0  0.523270  0.093147 -0.523270   -0.315650   \n",
       "3     David        1   30    Red  0  0.523270  0.093147 -0.523270   -0.315650   \n",
       "4      Eric        0   30  Green  1  0.523270  0.093147  0.476730   -0.315650   \n",
       "5  Felicity        0   10   Blue  1  0.869114  1.893147  0.130886   -0.315650   \n",
       "\n",
       "      $l_2$     $p_2$  \n",
       "0  1.640627  0.837620  \n",
       "1  1.621995  0.835070  \n",
       "2 -0.159373  0.460241  \n",
       "3 -0.159373  0.460241  \n",
       "4 -0.159373  0.460241  \n",
       "5  1.640627  0.837620  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"236pt\" height=\"165pt\"\r\n",
       " viewBox=\"0.00 0.00 235.50 165.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 161)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-161 231.5,-161 231.5,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#fbeade\" stroke=\"black\" d=\"M153.5,-157C153.5,-157 75.5,-157 75.5,-157 69.5,-157 63.5,-151 63.5,-145 63.5,-145 63.5,-101 63.5,-101 63.5,-95 69.5,-89 75.5,-89 75.5,-89 153.5,-89 153.5,-89 159.5,-89 165.5,-95 165.5,-101 165.5,-101 165.5,-145 165.5,-145 165.5,-151 159.5,-157 153.5,-157\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"114.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">age &lt;= 60.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"114.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.172</text>\r\n",
       "<text text-anchor=\"middle\" x=\"114.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 6</text>\r\n",
       "<text text-anchor=\"middle\" x=\"114.5\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.028</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M95,-53C95,-53 12,-53 12,-53 6,-53 0,-47 0,-41 0,-41 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 95,-0 95,-0 101,-0 107,-6 107,-12 107,-12 107,-41 107,-41 107,-47 101,-53 95,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"53.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.158</text>\r\n",
       "<text text-anchor=\"middle\" x=\"53.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"53.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = &#45;0.062</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M93.1652,-88.9485C87.4963,-80.1664 81.3618,-70.6629 75.6504,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"78.477,-59.7402 70.1131,-53.2367 72.5959,-63.5365 78.477,-59.7402\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"64.8481\" y=\"-73.976\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M215.5,-53C215.5,-53 137.5,-53 137.5,-53 131.5,-53 125.5,-47 125.5,-41 125.5,-41 125.5,-12 125.5,-12 125.5,-6 131.5,-0 137.5,-0 137.5,-0 215.5,-0 215.5,-0 221.5,-0 227.5,-6 227.5,-12 227.5,-12 227.5,-41 227.5,-41 227.5,-47 221.5,-53 215.5,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"176.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"176.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"middle\" x=\"176.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.477</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>0&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M136.185,-88.9485C141.946,-80.1664 148.181,-70.6629 153.986,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"157.055,-63.5178 159.615,-53.2367 151.203,-59.6779 157.055,-63.5178\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"164.698\" y=\"-74.0145\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x1f1661b8188>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iteration(1)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For Iteration 2, we have two leaves. <br />\n",
    "For Leaf 1, there are 5 samples. And $\\gamma$ is\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 303,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.3156498224562022"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sum_of_residual=0.130886+-0.523270+-0.523270+0.476730+0.130886\n",
    "denominator=2*0.869114*(1-0.869114)+3*0.523270*(1-0.523270)\n",
    "display(sum_of_residual/denominator)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For Leaf 2, there is only 1 sample. And $\\gamma$ is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.9110593001700842"
      ]
     },
     "execution_count": 304,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.476730/(0.523270*(1-0.523270))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 305,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 2 2 2 1]\n",
      "for leaf 1, we have 2 related samples. and gamma is 1.1938583610371274\n",
      "for leaf 2, we have 4 related samples. and gamma is -0.24439014590890917\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>popcorn</th>\n",
       "      <th>age</th>\n",
       "      <th>color</th>\n",
       "      <th>y</th>\n",
       "      <th>$p_2$</th>\n",
       "      <th>$l_2$</th>\n",
       "      <th>$r_2$</th>\n",
       "      <th>$\\gamma_2$</th>\n",
       "      <th>$l_3$</th>\n",
       "      <th>$p_3$</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alex</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.837620</td>\n",
       "      <td>1.640627</td>\n",
       "      <td>0.162380</td>\n",
       "      <td>1.193858</td>\n",
       "      <td>2.595714</td>\n",
       "      <td>0.930585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Brunei</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.835070</td>\n",
       "      <td>1.621995</td>\n",
       "      <td>0.164930</td>\n",
       "      <td>-0.244390</td>\n",
       "      <td>1.426483</td>\n",
       "      <td>0.806353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Candy</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Blue</td>\n",
       "      <td>0</td>\n",
       "      <td>0.460241</td>\n",
       "      <td>-0.159373</td>\n",
       "      <td>-0.460241</td>\n",
       "      <td>-0.244390</td>\n",
       "      <td>-0.354885</td>\n",
       "      <td>0.412198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>David</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>Red</td>\n",
       "      <td>0</td>\n",
       "      <td>0.460241</td>\n",
       "      <td>-0.159373</td>\n",
       "      <td>-0.460241</td>\n",
       "      <td>-0.244390</td>\n",
       "      <td>-0.354885</td>\n",
       "      <td>0.412198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eric</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.460241</td>\n",
       "      <td>-0.159373</td>\n",
       "      <td>0.539759</td>\n",
       "      <td>-0.244390</td>\n",
       "      <td>-0.354885</td>\n",
       "      <td>0.412198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felicity</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.837620</td>\n",
       "      <td>1.640627</td>\n",
       "      <td>0.162380</td>\n",
       "      <td>1.193858</td>\n",
       "      <td>2.595714</td>\n",
       "      <td>0.930585</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name  popcorn  age  color  y     $p_2$     $l_2$     $r_2$  $\\gamma_2$  \\\n",
       "0      Alex        1   10   Blue  1  0.837620  1.640627  0.162380    1.193858   \n",
       "1    Brunei        1   90  Green  1  0.835070  1.621995  0.164930   -0.244390   \n",
       "2     Candy        0   30   Blue  0  0.460241 -0.159373 -0.460241   -0.244390   \n",
       "3     David        1   30    Red  0  0.460241 -0.159373 -0.460241   -0.244390   \n",
       "4      Eric        0   30  Green  1  0.460241 -0.159373  0.539759   -0.244390   \n",
       "5  Felicity        0   10   Blue  1  0.837620  1.640627  0.162380    1.193858   \n",
       "\n",
       "      $l_3$     $p_3$  \n",
       "0  2.595714  0.930585  \n",
       "1  1.426483  0.806353  \n",
       "2 -0.354885  0.412198  \n",
       "3 -0.354885  0.412198  \n",
       "4 -0.354885  0.412198  \n",
       "5  2.595714  0.930585  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"236pt\" height=\"165pt\"\r\n",
       " viewBox=\"0.00 0.00 235.50 165.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 161)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-161 231.5,-161 231.5,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#f6d5bd\" stroke=\"black\" d=\"M151,-157C151,-157 73,-157 73,-157 67,-157 61,-151 61,-145 61,-145 61,-101 61,-101 61,-95 67,-89 73,-89 73,-89 151,-89 151,-89 157,-89 163,-95 163,-101 163,-101 163,-145 163,-145 163,-151 157,-157 151,-157\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">age &lt;= 20.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.132</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 6</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.018</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M90,-53C90,-53 12,-53 12,-53 6,-53 0,-47 0,-41 0,-41 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 90,-0 90,-0 96,-0 102,-6 102,-12 102,-12 102,-41 102,-41 102,-47 96,-53 90,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.162</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M90.6652,-88.9485C84.9963,-80.1664 78.8618,-70.6629 73.1504,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"75.977,-59.7402 67.6131,-53.2367 70.0959,-63.5365 75.977,-59.7402\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"62.3481\" y=\"-73.976\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M215.5,-53C215.5,-53 132.5,-53 132.5,-53 126.5,-53 120.5,-47 120.5,-41 120.5,-41 120.5,-12 120.5,-12 120.5,-6 126.5,-0 132.5,-0 132.5,-0 215.5,-0 215.5,-0 221.5,-0 227.5,-6 227.5,-12 227.5,-12 227.5,-41 227.5,-41 227.5,-47 221.5,-53 215.5,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.183</text>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 4</text>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = &#45;0.054</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>0&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M133.685,-88.9485C139.446,-80.1664 145.681,-70.6629 151.486,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"154.555,-63.5178 157.115,-53.2367 148.703,-59.6779 154.555,-63.5178\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"162.198\" y=\"-74.0145\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x1f165db6088>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iteration(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 306,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 1 1 1 1]\n",
      "for leaf 1, we have 5 related samples. and gamma is -0.11420357619360062\n",
      "for leaf 2, we have 1 related samples. and gamma is 1.2401521534064346\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>popcorn</th>\n",
       "      <th>age</th>\n",
       "      <th>color</th>\n",
       "      <th>y</th>\n",
       "      <th>$p_3$</th>\n",
       "      <th>$l_3$</th>\n",
       "      <th>$r_3$</th>\n",
       "      <th>$\\gamma_3$</th>\n",
       "      <th>$l_4$</th>\n",
       "      <th>$p_4$</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alex</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.930585</td>\n",
       "      <td>2.595714</td>\n",
       "      <td>0.069415</td>\n",
       "      <td>-0.114204</td>\n",
       "      <td>2.504351</td>\n",
       "      <td>0.924446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Brunei</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.806353</td>\n",
       "      <td>1.426483</td>\n",
       "      <td>0.193647</td>\n",
       "      <td>1.240152</td>\n",
       "      <td>2.418604</td>\n",
       "      <td>0.918235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Candy</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Blue</td>\n",
       "      <td>0</td>\n",
       "      <td>0.412198</td>\n",
       "      <td>-0.354885</td>\n",
       "      <td>-0.412198</td>\n",
       "      <td>-0.114204</td>\n",
       "      <td>-0.446247</td>\n",
       "      <td>0.390253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>David</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>Red</td>\n",
       "      <td>0</td>\n",
       "      <td>0.412198</td>\n",
       "      <td>-0.354885</td>\n",
       "      <td>-0.412198</td>\n",
       "      <td>-0.114204</td>\n",
       "      <td>-0.446247</td>\n",
       "      <td>0.390253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eric</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.412198</td>\n",
       "      <td>-0.354885</td>\n",
       "      <td>0.587802</td>\n",
       "      <td>-0.114204</td>\n",
       "      <td>-0.446247</td>\n",
       "      <td>0.390253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felicity</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.930585</td>\n",
       "      <td>2.595714</td>\n",
       "      <td>0.069415</td>\n",
       "      <td>-0.114204</td>\n",
       "      <td>2.504351</td>\n",
       "      <td>0.924446</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name  popcorn  age  color  y     $p_3$     $l_3$     $r_3$  $\\gamma_3$  \\\n",
       "0      Alex        1   10   Blue  1  0.930585  2.595714  0.069415   -0.114204   \n",
       "1    Brunei        1   90  Green  1  0.806353  1.426483  0.193647    1.240152   \n",
       "2     Candy        0   30   Blue  0  0.412198 -0.354885 -0.412198   -0.114204   \n",
       "3     David        1   30    Red  0  0.412198 -0.354885 -0.412198   -0.114204   \n",
       "4      Eric        0   30  Green  1  0.412198 -0.354885  0.587802   -0.114204   \n",
       "5  Felicity        0   10   Blue  1  0.930585  2.595714  0.069415   -0.114204   \n",
       "\n",
       "      $l_4$     $p_4$  \n",
       "0  2.504351  0.924446  \n",
       "1  2.418604  0.918235  \n",
       "2 -0.446247  0.390253  \n",
       "3 -0.446247  0.390253  \n",
       "4 -0.446247  0.390253  \n",
       "5  2.504351  0.924446  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"226pt\" height=\"165pt\"\r\n",
       " viewBox=\"0.00 0.00 226.00 165.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 161)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-161 222,-161 222,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#fbeade\" stroke=\"black\" d=\"M147,-157C147,-157 69,-157 69,-157 63,-157 57,-151 57,-145 57,-145 57,-101 57,-101 57,-95 63,-89 69,-89 69,-89 147,-89 147,-89 153,-89 159,-95 159,-101 159,-101 159,-145 159,-145 159,-151 153,-157 147,-157\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"108\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">age &lt;= 60.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"108\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.122</text>\r\n",
       "<text text-anchor=\"middle\" x=\"108\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 6</text>\r\n",
       "<text text-anchor=\"middle\" x=\"108\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.016</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M86,-53C86,-53 12,-53 12,-53 6,-53 0,-47 0,-41 0,-41 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 86,-0 86,-0 92,-0 98,-6 98,-12 98,-12 98,-41 98,-41 98,-47 92,-53 86,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"49\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.139</text>\r\n",
       "<text text-anchor=\"middle\" x=\"49\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"49\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = &#45;0.02</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M87.3647,-88.9485C81.8817,-80.1664 75.9483,-70.6629 70.4242,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"73.3333,-59.8656 65.0684,-53.2367 67.3955,-63.5728 73.3333,-59.8656\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"59.4353\" y=\"-73.8938\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M206,-53C206,-53 128,-53 128,-53 122,-53 116,-47 116,-41 116,-41 116,-12 116,-12 116,-6 122,-0 128,-0 128,-0 206,-0 206,-0 212,-0 218,-6 218,-12 218,-12 218,-41 218,-41 218,-47 212,-53 206,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"167\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = &#45;0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"167\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"middle\" x=\"167\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.194</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>0&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M128.635,-88.9485C134.118,-80.1664 140.052,-70.6629 145.576,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"148.604,-63.5728 150.932,-53.2367 142.667,-59.8656 148.604,-63.5728\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"156.565\" y=\"-73.8938\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x1f1661bbbc8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iteration(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 307,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 2 1 2 1 1]\n",
      "for leaf 1, we have 3 related samples. and gamma is 0.5406201302738854\n",
      "for leaf 2, we have 3 related samples. and gamma is -0.6083743125192569\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>popcorn</th>\n",
       "      <th>age</th>\n",
       "      <th>color</th>\n",
       "      <th>y</th>\n",
       "      <th>$p_4$</th>\n",
       "      <th>$l_4$</th>\n",
       "      <th>$r_4$</th>\n",
       "      <th>$\\gamma_4$</th>\n",
       "      <th>$l_5$</th>\n",
       "      <th>$p_5$</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alex</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.924446</td>\n",
       "      <td>2.504351</td>\n",
       "      <td>0.075554</td>\n",
       "      <td>-0.608374</td>\n",
       "      <td>2.017652</td>\n",
       "      <td>0.882638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Brunei</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.918235</td>\n",
       "      <td>2.418604</td>\n",
       "      <td>0.081765</td>\n",
       "      <td>-0.608374</td>\n",
       "      <td>1.931905</td>\n",
       "      <td>0.873460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Candy</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Blue</td>\n",
       "      <td>0</td>\n",
       "      <td>0.390253</td>\n",
       "      <td>-0.446247</td>\n",
       "      <td>-0.390253</td>\n",
       "      <td>0.540620</td>\n",
       "      <td>-0.013751</td>\n",
       "      <td>0.496562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>David</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>Red</td>\n",
       "      <td>0</td>\n",
       "      <td>0.390253</td>\n",
       "      <td>-0.446247</td>\n",
       "      <td>-0.390253</td>\n",
       "      <td>-0.608374</td>\n",
       "      <td>-0.932947</td>\n",
       "      <td>0.282327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eric</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>Green</td>\n",
       "      <td>1</td>\n",
       "      <td>0.390253</td>\n",
       "      <td>-0.446247</td>\n",
       "      <td>0.609747</td>\n",
       "      <td>0.540620</td>\n",
       "      <td>-0.013751</td>\n",
       "      <td>0.496562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Felicity</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>Blue</td>\n",
       "      <td>1</td>\n",
       "      <td>0.924446</td>\n",
       "      <td>2.504351</td>\n",
       "      <td>0.075554</td>\n",
       "      <td>0.540620</td>\n",
       "      <td>2.936847</td>\n",
       "      <td>0.949638</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name  popcorn  age  color  y     $p_4$     $l_4$     $r_4$  $\\gamma_4$  \\\n",
       "0      Alex        1   10   Blue  1  0.924446  2.504351  0.075554   -0.608374   \n",
       "1    Brunei        1   90  Green  1  0.918235  2.418604  0.081765   -0.608374   \n",
       "2     Candy        0   30   Blue  0  0.390253 -0.446247 -0.390253    0.540620   \n",
       "3     David        1   30    Red  0  0.390253 -0.446247 -0.390253   -0.608374   \n",
       "4      Eric        0   30  Green  1  0.390253 -0.446247  0.609747    0.540620   \n",
       "5  Felicity        0   10   Blue  1  0.924446  2.504351  0.075554    0.540620   \n",
       "\n",
       "      $l_5$     $p_5$  \n",
       "0  2.017652  0.882638  \n",
       "1  1.931905  0.873460  \n",
       "2 -0.013751  0.496562  \n",
       "3 -0.932947  0.282327  \n",
       "4 -0.013751  0.496562  \n",
       "5  2.936847  0.949638  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"236pt\" height=\"165pt\"\r\n",
       " viewBox=\"0.00 0.00 235.50 165.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 161)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-161 231.5,-161 231.5,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\r\n",
       "<path fill=\"#f2c09c\" stroke=\"black\" d=\"M175,-157C175,-157 49,-157 49,-157 43,-157 37,-151 37,-145 37,-145 37,-101 37,-101 37,-95 43,-89 49,-89 49,-89 175,-89 175,-89 181,-89 187,-95 187,-101 187,-101 187,-145 187,-145 187,-151 181,-157 175,-157\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">likes_popcorn &lt;= 0.5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.116</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 6</text>\r\n",
       "<text text-anchor=\"middle\" x=\"112\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.01</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M90,-53C90,-53 12,-53 12,-53 6,-53 0,-47 0,-41 0,-41 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 90,-0 90,-0 96,-0 102,-6 102,-12 102,-12 102,-41 102,-41 102,-47 96,-53 90,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.167</text>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"middle\" x=\"51\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.098</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M90.6652,-88.9485C84.9963,-80.1664 78.8618,-70.6629 73.1504,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"75.977,-59.7402 67.6131,-53.2367 70.0959,-63.5365 75.977,-59.7402\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"62.3481\" y=\"-73.976\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M215.5,-53C215.5,-53 132.5,-53 132.5,-53 126.5,-53 120.5,-47 120.5,-41 120.5,-41 120.5,-12 120.5,-12 120.5,-6 126.5,-0 132.5,-0 132.5,-0 215.5,-0 215.5,-0 221.5,-0 227.5,-6 227.5,-12 227.5,-12 227.5,-41 227.5,-41 227.5,-47 221.5,-53 215.5,-53\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mse = 0.049</text>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"middle\" x=\"174\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = &#45;0.078</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>0&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M133.685,-88.9485C139.446,-80.1664 145.681,-70.6629 151.486,-61.815\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"154.555,-63.5178 157.115,-53.2367 148.703,-59.6779 154.555,-63.5178\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"162.198\" y=\"-74.0145\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x1f165da3fc8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "iteration(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 308,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD4CAYAAADlwTGnAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAbEklEQVR4nO3de5Bc9Xnm8e8joQsgQBIaCVkSSKBBF9trYY+1iTEkCIbIXi+wtS5Hqji2t1xhs2WcxE45hlqHsKxdtfln2doUSRVxsNeXoLDErFVreaWWBfgSYTQy4qIeJAYJrEFSz4Du1nWkd//o09Aa9UgtTfec7j7Pp6pr5vz6d47eUzD9dP/69NuKCMzMLHtGpV2AmZmlwwFgZpZRDgAzs4xyAJiZZZQDwMwsoy5Ku4DzMWXKlJg9e3baZZiZNZWNGze+FRFtg8ebKgBmz55NV1dX2mWYmTUVSW9UGvcSkJlZRjkAzMwyygFgZpZRDgAzs4xyAJiZZZQDwMwsoxwAZmYZ1VSfAzA7mx9uepPX+g6lXYZZXXz2I7O5csK4mh7TAWAtof/gMf7snzYRA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10QWAmAD0C5pjqSxwDJgZco1WQ0lb4j+A9AdEf897XpGgqQ2SROT3y8GbgNeSbeq+oqI+yJiZkTMpvh3vC4iPp1yWXUl6dLkwgYkXQrcDtTs6r6WD4CIGADuAVZTfHPw8YjYnG5V9SXpMWA9ME9Sr6TPp11Tnd0I/CHFZ4SbktvH0y6qzqYDT0l6keKTnFxEZOKyyIyZBvxc0gvAc8CPIuL/1ergLX8ZqJmZVdbyrwDMzKwyB4CZWUY5AMzMMsoBYGaWUQ4AM7OMcgCYmWWUA8DMLKP+P03oENd+TZq3AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(score)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3deXSdZbn38e+VqWnSKWnSgTZp0oEWKG3aho6KWBGLKCBjK0Jxqvh6GPR4FD2+IqjvQXExeY5ARaSIUgZREGSoMglp06bzSOmQzrRpk6ZN26QZrvePvdsTY9LsNMOzs/P7rLVX936e+8m+9nL5y8OVe9+3uTsiIhK74oIuQERE2peCXkQkxinoRURinIJeRCTGKehFRGJcQtAFNCYjI8NzcnKCLkNEpNNYunTpfnfPbOxcVAZ9Tk4ORUVFQZchItJpmNm2ps6pdSMiEuMU9CIiMU5BLyIS4xT0IiIxTkEvIhLjFPQiIjFOQS8iEuNiJuhr65z/eXMTK3ccDLoUEZGoEjNBX1FVw+8XbeOW+cupqKoJuhwRkagRM0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3CsBH3EO1lJ59fMbmcxx91r3T2P0MY9E80sptt0ZvYZYJ+7Lw26lg42zd3HAxcD3wi3ZttErAS9drLqIiLY7SxmuftB4C1gRsCltLdpwKXhnvV8YLqZPRlsSe3P3XeH/90H/IlQS7pNxErQLwFGmFmumSUBMwnteiUxJMLdzmKKmWWaWZ/w8+7AhcCGYKtqX+7+PXcf7O45hP6//Ia7fyHgstqVmaWGJxhgZqmEduNrs9l0MRH07l4D/BuhbQrXA8+4+9pgq2p/ZvYUsBAYaWY7zezLQdfUzk7sdjY9PAVthZl9Ouii2tlA4E0zW0XohmaBu3eJ6YZdTH/gXTNbCSwGXnb3V9vqh8fE9EoREWlaTNzRi4hI0xT0IiIxTkEvIhLjFPQiIjFOQS8iEuMU9CIiMU5BLyIS4/4/acuh1TaRNbUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(loss)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example 2\n",
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "357 out of 569 persons have cancer\n",
      "features 30\n"
     ]
    }
   ],
   "source": [
    "cancer=load_breast_cancer()\n",
    "n_samples = len(cancer.target)\n",
    "X = cancer.data\n",
    "y = cancer.target\n",
    "total=len(y)\n",
    "has_cancer=np.sum(y)\n",
    "no_cancer=total-has_cancer\n",
    "print(f'{has_cancer} out of {total} persons have cancer')\n",
    "print(f'features {X.shape[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1 Log of Odds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the log_of_odds is : 0.5211495071076265\n",
      "the probability is : 0.6274165202108963\n"
     ]
    }
   ],
   "source": [
    "log_of_odds0=np.log(has_cancer / no_cancer)\n",
    "probability0=np.exp(log_of_odds0)/(np.exp(log_of_odds0)+1)\n",
    "print(f'the log_of_odds is : {log_of_odds0}')\n",
    "print(f'the probability is : {probability0}')\n",
    "predict0=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2 For Loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loss is 375.7200026920845\n",
      "loss is 375.72000269208445\n",
      "gamma1 1.2213642015774735\n",
      "gamma2 -2.4363001705150817\n",
      "new loss is 338.13703383104206\n"
     ]
    }
   ],
   "source": [
    "#explore\n",
    "residuals0=y - probability0\n",
    "loss0=-(y*np.log(probability0)+(1-y)*np.log(1-probability0))\n",
    "print(f'loss is {loss0.sum()}')\n",
    "loss0=-y * log_of_odds0 + np.log(1+np.exp(log_of_odds0))\n",
    "print(f'loss is {loss0.sum()}')\n",
    "\n",
    "tree0 = DecisionTreeRegressor(max_depth=1)\n",
    "tree0.fit(X, residuals0)\n",
    "stem = tree0.tree_\n",
    "\n",
    "leaf_index=tree0.apply(X)\n",
    "#for leaf 1\n",
    "n_leaf1=len(leaf_index[leaf_index==1])\n",
    "previous_probability1 = np.array([probability0] * n_leaf1)\n",
    "denominator1 = np.sum(previous_probability1 * (1-previous_probability1))\n",
    "gamma1 = stem.value[1][0][0] * n_leaf1 / denominator1\n",
    "print(f'gamma1 {gamma1}')\n",
    "#for leaf 2\n",
    "n_leaf2=len(leaf_index[leaf_index==2])\n",
    "previous_probability2 = np.array([probability0] * n_leaf2)\n",
    "denominator2 = np.sum(previous_probability2 * (1-previous_probability2))\n",
    "gamma2 = stem.value[2][0][0] * n_leaf2 / denominator2\n",
    "print(f'gamma2 {gamma2}')\n",
    "\n",
    "gamma_value=[gamma1, gamma2]\n",
    "gamma = np.array([gamma_value[index-1] for index in leaf_index])\n",
    "new_log_of_odds = log_of_odds0 + 0.1 * gamma\n",
    "new_loss=-y * new_log_of_odds + np.log(1+np.exp(new_log_of_odds))\n",
    "print(f'new loss is {new_loss.sum()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {},
   "outputs": [],
   "source": [
    "#now let's get started\n",
    "n_times = 20\n",
    "learning_rate=0.1\n",
    "log_of_odds=np.zeros([n_times+1,n_samples])\n",
    "\n",
    "residuals = np.zeros([n_times+1,n_samples])\n",
    "loss = [0] * (n_times+1)\n",
    "probabilities = np.zeros([n_times+1,n_samples])\n",
    "score = np.zeros([n_times+1])\n",
    "#calculation\n",
    "log_of_odds[0] = [log_of_odds0] * n_samples\n",
    "probabilities[0] = [probability0] * n_samples\n",
    "score[0]=np.sum((probabilities[0]>0.5)*1.0==y) / n_samples\n",
    "residuals[0] = y - probabilities[0]\n",
    "loss[0] = loss0.sum()\n",
    "trees = []\n",
    "gamma_value = np.zeros([n_times+1,8])\n",
    "gamma = np.zeros([n_times+1,n_samples])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(n_times):\n",
    "    dt = DecisionTreeRegressor(max_depth=2)\n",
    "    dt=dt.fit(X, residuals[i])\n",
    "    tree = dt.tree_\n",
    "    trees.append(tree)\n",
    "    leaf_indeces=dt.apply(X)\n",
    "    unique_leaves=np.unique(leaf_indeces)\n",
    "    n_leaf=len(unique_leaves)\n",
    "    #for leaf 1\n",
    "    for ileaf in range(n_leaf):\n",
    "        leaf_index=unique_leaves[ileaf]\n",
    "        n_leaf=len(leaf_indeces[leaf_indeces==leaf_index])\n",
    "        previous_probability = probabilities[i][leaf_indeces==leaf_index]\n",
    "        denominator = np.sum(previous_probability * (1-previous_probability))\n",
    "        igamma = tree.value[ileaf+1][0][0] * n_leaf / denominator\n",
    "        gamma_value[i][ileaf]=igamma\n",
    "\n",
    "    gamma[i] = [gamma_value[i][np.where(unique_leaves==index)] for index in leaf_indeces]\n",
    "    #next \n",
    "    log_of_odds[i+1] = log_of_odds[i] + learning_rate * gamma[i]\n",
    "\n",
    "    probabilities[i+1] = np.array([np.exp(odds)/(np.exp(odds)+1) for odds in log_of_odds[i+1]])\n",
    "    score[i+1]=np.sum((probabilities[i+1]>0.5)*1.0==y) / n_samples\n",
    "    residuals[i+1] = y - probabilities[i+1]\n",
    "    loss[i+1]=np.sum(-y * log_of_odds[i+1] + np.log(1+np.exp(log_of_odds[i+1])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3deXxU5b3H8c8vO4GQEBJCgLCHXdaIIEWoVo1LxVattlZR2+JarbdX7XLb2u32qldtvXXDpWLdl6q44S6himBAwCAQwh4gC2uAQEKS5/4xB5tiQhLI5Exmvu/Xa15z5jnnZH45M/nmzDPnPMecc4iISHiJ8rsAERFpfQp3EZEwpHAXEQlDCncRkTCkcBcRCUMxfhcAkJaW5vr27et3GSIi7cqiRYu2OefSG5oXEuHet29f8vPz/S5DRKRdMbMNjc1Tt4yISBhSuIuIhCGFu4hIGFK4i4iEIYW7iEgYUriLiIQhhbuISBhq1+G+cXslt85ezsHaOr9LEREJKe063AtL9/DYx+t5euFGv0sREQkp7TrcTxnajQn9U/nzu6upOHDQ73JEREJGuw53M+OXZw5jx75qHvhwjd/liIiEjHYd7gDH9UrmW2N68sg/17Fl136/yxERCQntPtwBfnraIBzwv2+v8rsUEZGQEBbh3qtLIldM6sdLn22mYPNuv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ln5A4Hua94DV3n2qt2wO8HC9da/w3mtFBEY/DXZdRQT6YQ+9zw4dGdYDeONIr3uQ6/q79/5ZRiC4Mg+vy3v8lb/fYNbltT926H1Vb9k22V5HyIY2e39p+AERkTDUXrplRESkBRTuIiJhSOEuIhKGFO4iImFI4S4iEoYU7iIiYUjhLiIShv4fmNh13DBIjkoAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(loss)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3de3xcZ33n8c9Pd8mWZdmSncTyLYkNCYTc9HISAiElJDF5sSRcCg6QhpZtForTJUB3w7abpqZdUuhCdyFcAnUJtxjK1bDuhiy5mFKHWM7FiR1iK44vio1mLFnWWLJGt9/+MUfyWB5Zo2jmnNHo+3695qWZc54z88yZ0VePnvM855i7IyIixask6gqIiEh+KehFRIqcgl5EpMgp6EVEipyCXkSkyJVFXYGxGhoafNmyZVFXQ0RkWtm2bdthd2/MtK7ggn7ZsmW0tLREXQ0RkWnFzPaNt05dNyIiRU5BLyJS5BT0IiJFTkEvIlLkFPQiIkVOQS8iUuQU9CIiRa7gxtGLiMw0B7uOs3lXnCF33n/Z0pw/v4JeRCRkfQNDPL6ng827DrN5d5zW2DEALl4yN7qgN7PVwP8CSoFvuPs9Y9YvBdYDjUAn8AF3bwvWDQHPBkX3u/vbc1R3EZFpwd3ZHTvG5l1xHtsV54mXOkkODlN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sw7JJ7bPpGPRtwOK0x03AwfHKmFkZUMcr+zdzUsysnNSH+F13//HY9e7e7e7HgvubgHIza8h3vYLXOxj8jAE/IfUvdLps9mu+vBV40t3bx66Icp8B7SPdV8HPWIYykey34IDc24D3e9CRO1YWn3lOuXu7uw+5+zDw9XFeL6r9VQa8E/j+eGXyvb/GyYdQvmPTMei3AivMbHnQElwDbBxTZiMwcmT63cDD4/0y5ErQ//dPwPPu/vlxypwxcqzAzFaR2v8d+axX8FqzzKx25D6pg3nPjSm2EfgjS7kcODryL2UIxm1pRbXPAunfo1uBn2Uo8yBwnZnVB10V1wXL8sbMVgP/FXi7u/eOUyabzzzX9Uo/pvOOcV4vm9/ffHgL8Dt3b8u0Mt/76zT5EM53LB9HmPN9IzVCZBepo/d/GSxbR+qLD1BFqhugFXgCODuEOr2B1L9T24Gng9sNwIeBDwdl1gI7SI00eBx4fUj76+zgNZ8JXn9kn6XXzYB7g336LNAcUt1qSAV3Xdqy0PcZqT80h4ABUi2oD5E6rvMrYHfwc15Qthn4Rtq2fxJ811qBPw6hXq2k+mxHvmcjI8zOAjad7jPPc72+HXx3tpMKsDPH1it4fMrvbz7rFSz/5sh3Kq1smPtrvHwI5TumU3WnQUEAAAA0SURBVCCIiBS56dh1IyIik6CgFxEpcgp6EZEip6AXESlyCnoRkSKnoBcRKXIKehGRIvf/AYifWf5EzIsVAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(score)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reference\n",
    "\n",
    "[Gradient Boosting (Wikipedia)](https://en.wikipedia.org/wiki/Gradient_boosting)\n",
    "\n",
    "[Gradient Boost Part 3: Classification -- Youtube StatQuest](https://www.youtube.com/watch?v=jxuNLH5dXCs)\n",
    "\n",
    "[Gradient Boost Part 4: Classification Details -- Youtube StatQuest](https://www.youtube.com/watch?v=StWY5QWMXCw)\n",
    "\n",
    "[sklearn.tree.DecisionTreeRegressor -- scikit-learn 0.21.3 documentation](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html)\n",
    "\n",
    "[Understanding the decision tree structure -- scikit-learn 0.21.3 documentation](https://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
